﻿using System;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using Titan.DAS.Client.Data;
using Titan.FIS.Enum;

namespace Titan.FIS.LmsBalanceCollect
{
	public class DataQuery
	{
		private static string _strSql = string.Empty;

        public static bool DoQueryTestCodeAny(string strAsNo)
        {
            _strSql = $@"SELECT * FROM LMS_COLLECTSAMPLE WHERE TEST_CODE = '{strAsNo}'";
            var modelQuery = DataHelper.Instance.QuerySql(Common.UserInfo.UserName, Common.UserInfo.UserPwd, _strSql);
            if (modelQuery.RSuccess)
            {
                if (modelQuery.RSource != null && modelQuery.RSource.Tables.Count > 0 && modelQuery.RSource.Tables[0].Rows.Count > 0)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }


        /// <summary>
        /// 按条件查询化验数据信息
        /// </summary>
        public static string DoQueryTestDataInfo(string strAsNo,  string strStartTime, string strEndTime, ref DataTable tabSource)
		{
            var errMsg = string.Empty;
            _strSql = @"SELECT ROWNUM AS F_NO, C.*
						   FROM (SELECT to_char(INSTRUMENT) as INSTRUMENT,
                               AUTO_NO,
                               TEST_CODE,
                               SAMPLE_WEIGHT,
                               SAMPLE_COUNT,
                               CRI_WEIGHT,
                               CRI_COUNT,
                               CSR_WEIGHT,
                               CSR_COUNT,
                               CRI,
                               CSR,
                               REMARK,
                               B.TRUENAME AS TEST_USER,
                               TO_CHAR(TEST_TIME,'YYYY-MM-DD HH24:MI:SS') AS TEST_TIME
                          FROM LMS_TESTDATA_CRI A
                          LEFT JOIN SYSUSER B
                            ON A.TEST_USER = B.ID
                         WHERE 1 = 1 ";
            if (!string.IsNullOrEmpty(strAsNo)) _strSql += " and TEST_CODE = '" + strAsNo + "'";
            if (!string.IsNullOrEmpty(strStartTime)) _strSql += " and TEST_TIME >= to_date('" + strStartTime + "','yyyy-mm-dd hh24:mi:ss')";
            if (!string.IsNullOrEmpty(strEndTime)) _strSql += " and TEST_TIME <= to_date('" + strEndTime + "','yyyy-mm-dd hh24:mi:ss')";
            _strSql += " ORDER BY TEST_TIME DESC) C";
            var modelQuery = DataHelper.Instance.QuerySql(Common.UserInfo.UserName, Common.UserInfo.UserPwd, _strSql);
            if (modelQuery.RSuccess)
            {
                if (modelQuery.RSource != null && modelQuery.RSource.Tables.Count > 0 && modelQuery.RSource.Tables[0].Rows.Count > 0)
                    tabSource = modelQuery.RSource.Tables[0];
                else
                    errMsg = "查询完毕，未找到相关数据，请更换条件后在查询！";
            }
            else
                errMsg = string.Format("查询化验数据失败！\r\n错误代码:{0}  错误信息:{1}", modelQuery.RStatuCode, modelQuery.RErrInfo + modelQuery.RStatuDesc);
            return errMsg;
        }


	}
}
